//
//  SCNavigationController.m
//  Shuaicheyeguan
//
//  Created by xiangtao on 2025/3/26.
//

#import "SCNavigationController.h"

@interface SCNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@end

@implementation SCNavigationController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationBar.translucent = NO;
  self.interactivePopGestureRecognizer.delegate = nil;
  __weak SCNavigationController *weakSelf = self;
  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  {
    self.interactivePopGestureRecognizer.delegate = weakSelf;
    self.delegate = weakSelf;
  }
  
  if (@available(iOS 15.0, *)) {
    UINavigationBarAppearance *navigationBarAppearance = [UINavigationBarAppearance new];
    navigationBarAppearance.backgroundColor = [UIColor whiteColor];
    navigationBarAppearance.backgroundEffect = nil;
    
    self.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
    self.navigationBar.standardAppearance = navigationBarAppearance;
  }
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
  if (self.viewControllers.count > 0) {
    viewController.hidesBottomBarWhenPushed = YES;
  }
  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = NO;
  [super pushViewController:viewController animated:animated];
}

// 跟控制器是导航控制器，那么在这个导航控制器中实现下面三个方法
-(BOOL)shouldAutorotate {
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count <= 1) {
        self.interactivePopGestureRecognizer.enabled = NO;
    } else{
        if (self.forbidPopGestureRecognizer) {
            self.interactivePopGestureRecognizer.enabled = NO;
        } else {
            self.interactivePopGestureRecognizer.delegate = (id)self;
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

- (void)setForbidPopGestureRecognizer:(BOOL)forbidPopGestureRecognizer
{
    _forbidPopGestureRecognizer = forbidPopGestureRecognizer;
    if (_forbidPopGestureRecognizer) {
        self.interactivePopGestureRecognizer.enabled = NO;
    } else {
        self.interactivePopGestureRecognizer.delegate = (id)self;
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.topViewController;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
